package com.yusheng.tr.multi.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yusheng.domain.R;
import com.yusheng.tr.common.core.model.dto.multi.ReportInfoPageDto;
import com.yusheng.tr.common.core.model.dto.multi.ReportTypePageDto;
import com.yusheng.tr.common.core.page.PageParam;
import com.yusheng.tr.common.core.entity.multi.ReportInfoEntity;
import com.yusheng.tr.common.core.entity.multi.ReportTypeEntity;
import com.yusheng.tr.multi.service.ReportInfoService;
import com.yusheng.tr.multi.service.ReportTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/report")
//@Api( tags = "报表管理功能")
@Tag(name = "报表管理功能",description = "报表管理功能")
public class ReportController {

    @Autowired
    private ReportTypeService typeService;

    @Autowired
    private ReportInfoService infoService;

    @PostMapping("/type/add/or/update")
    @Operation(summary = "新增或者修改报表类型",description = "新增或者修改报表类型")
    @Parameter(name = "entity",description = "新增或修改结算单实体参数")
    public R<?> addOrUpdate(@Valid @RequestBody ReportTypeEntity entity){
        typeService.saveOrUpdate(entity);
        return R.ok();
    }

    @GetMapping("/type/delete/by/{id}")
    @Operation(summary = "删除报表类型",description = "删除报表类型")
    @Parameter(name = "id",description = "删除结算单的id")
    public R<?> deleteById(@Valid @NotEmpty(message = "id不能为空") @PathVariable String id){
        typeService.removeById(id);
        return R.ok();
    }

    @PostMapping("/type/page/list")
    @Operation(summary = "分页条件查询",description = "分页条件查询")
    @Parameter(name = "dto",description = "分页查询的条件参数")
    public R<?> pageList(@RequestBody PageParam<ReportTypePageDto> dto){
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        return R.ok(new PageInfo<>(typeService.list(new LambdaQueryWrapper<ReportTypeEntity>()
                .like(StringUtils.isNotEmpty(dto.getParam().getName()),ReportTypeEntity::getReportName,dto.getParam().getName())
                .eq(dto.getParam().getStatus()!=null,ReportTypeEntity::getStatus,dto.getParam().getStatus())
        )));
    }

    @PostMapping("/info/add/or/update")
    @Operation(summary = "新增或修改报表信息",description = "新增或修改报表信息")
    @Parameter(name = "entity",description = "报表信息实体参数")
    public R<?> addOrUpdate(@Valid @RequestBody ReportInfoEntity entity){
        infoService.saveOrUpdate(entity);
        return R.ok();
    }

    @GetMapping("/info/delete/by/{id}")
    @Operation(summary = "删除报表信息",description = "删除报表信息")
    @Parameter(name = "id",description = "删除报表信息的id")
    public R<?> deleteByIdInfo(@Valid @NotEmpty(message = "id不能为空") @PathVariable String id){
        infoService.removeById(id);
        return R.ok();
    }

    @PostMapping("/info/page/list")
    @Operation(summary = "分页条件查询",description = "分页条件查询")
    @Parameter(name = "dto",description = "分页条件")
    public R<?> pageInfoList(@RequestBody PageParam<ReportInfoPageDto> dto){
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        return R.ok(new PageInfo<>(infoService.list(new LambdaQueryWrapper<ReportInfoEntity>()
                .like(StringUtils.isNotEmpty(dto.getParam().getName()),ReportInfoEntity::getName,dto.getParam().getName())
                .eq(ReportInfoEntity::getReportTypeId,dto.getParam().getId())
        )));
    }
}
